package com.cskaoyan.controller.system;


import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketLog;
import com.cskaoyan.bean.po.MarketRole;
import com.cskaoyan.bean.vo.RoleManagementOptionsVo;
import com.cskaoyan.bean.vo.systemVo.PermissionsBo;
import com.cskaoyan.bean.vo.systemVo.PermissionsVo;
import com.cskaoyan.service.system.RoleManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 *
 */
@RestController
@RequestMapping("admin/role")
public class RoleManagement {

    @Autowired
    RoleManagementService roleManagementService;


    /**
     * 授权
     * @param
     * @return
     */
    @RequestMapping("permissions")
    public BaseRespVo<PermissionsVo>  permissions(@RequestParam("roleId") Integer roleId){
        PermissionsVo  permissionsVo= roleManagementService.selectPermissions(roleId);
        BaseRespVo<PermissionsVo> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        baseRespVo.setData(permissionsVo);
        return baseRespVo;
    }

    @PostMapping("permissions")
    public BaseRespVo<PermissionsVo>  permissionsPost(@RequestBody PermissionsBo permissionsBo){
        int i = roleManagementService.permissionsPost(permissionsBo);
        BaseRespVo<PermissionsVo> baseRespVo = new BaseRespVo<>();
        if (i==641){
            baseRespVo.setErrmsg("当前角色的超级权限不能变更");
            baseRespVo.setErrno(641);
            return baseRespVo;
        }

        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    /**
     * 删除角色
     * @param marketRole
     * @return
     */
    @RequestMapping("delete")
    public BaseRespVo<Object> delete(@RequestBody MarketRole marketRole){
         roleManagementService.deleteRole(marketRole);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }


    /**
     * 修改角色信息
     * @param marketRole
     * @return
     */
    @RequestMapping("update")
    public BaseRespVo<Object> update(@RequestBody MarketRole marketRole){
        MarketRole marketRole1 = roleManagementService.updateRole(marketRole);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    /**
     * 创建管理员
     * @param marketRole
     * @return
     */
    @RequestMapping("create")
    public BaseRespVo<MarketRole> create(@RequestBody MarketRole marketRole){
        MarketRole marketRole1 = roleManagementService.createRole(marketRole);
        BaseRespVo<MarketRole> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(marketRole1);
        return baseRespVo;

    }


    @RequestMapping("list")
    public BaseRespVo<CommonData<MarketRole>> list(@RequestParam("page")Integer page,
                                                   @RequestParam("limit")Integer limit,
                                                   @RequestParam("sort")String sort,
                                                   @RequestParam("order")String order,
                                                   HttpServletRequest request){
        String name = request.getParameter("name");
        CommonData<MarketRole> marketAdminCommonData = roleManagementService.selectCommonData(page, limit, sort, order,name);
        BaseRespVo<CommonData<MarketRole>> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        baseRespVo.setData(marketAdminCommonData);
        return baseRespVo;
    }


    /**
     * 展示角色信息
     * @return
     */
    @RequestMapping("options")
    public BaseRespVo<CommonData<RoleManagementOptionsVo>> options(){
      //响应{"errno":0,"data":{"total":4,"pages":1,"limit":4,"page":1,"list":[{"label":1,"label":"超级管理员"},{"label":2,"label":"商场管理员"},{"label":3,"label":"推广管理员"},{"label":4,"label":"无敌管理员"}]},"errmsg":"成功"}
        CommonData<RoleManagementOptionsVo> roleManagementOptionsVoCommonData = roleManagementService.selectOptions();
        BaseRespVo<CommonData<RoleManagementOptionsVo>> baseRespVo = new BaseRespVo<>();
        baseRespVo.setData(roleManagementOptionsVoCommonData);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }
}
